package edu.udlap.spring2008.is300.finalexam.framework.mvc.view;

import edu.udlap.spring2008.is300.finalexam.framework.mvc.controller.Controller;
import edu.udlap.spring2008.is300.finalexam.framework.mvc.model.ModelState;
import java.util.ArrayList;
import java.util.Iterator;

/**
 *
 * @author Ruslán Ledesma Garza
 */
public abstract class ViewCompositeComponent extends ViewComponent
{   
    // <editor-fold desc="composite management...">
    ArrayList<ViewComponent> children = new ArrayList<ViewComponent>();;
    
    public void addChild(ViewComponent child)
    {
        child.setController(controller_); ///set the controller of the child to be the controller of the parent...
        children.add(child);
    }
    
    public void removeChild(ViewComponent child)
    {
        children.remove(child);
    }
    // </editor-fold>
    
    // <editor-fold desc="implemented methods 'à la composite'...">
    public void setInitialState(ModelState modelState)
    {
        Iterator<ViewComponent> i = children.iterator();
        ViewComponent child;
        while(i.hasNext())
        {
            child = i.next();
            child.setInitialState(modelState);
        } 
    }
    
    public void imageSetChanged(ModelState modelState)
    {
        Iterator<ViewComponent> i = children.iterator();
        ViewComponent child;
        while(i.hasNext())
        {
            child = i.next();
            child.imageSetChanged(modelState);
        } 
    }
    
    public void selectionChanged(ModelState modelState)
    {
        Iterator<ViewComponent> i = children.iterator();
        ViewComponent child;
        while(i.hasNext())
        {
            child = i.next();
            child.selectionChanged(modelState);
        } 
    }
    
    public void favoritesChanged(ModelState modelState)
    {
        Iterator<ViewComponent> i = children.iterator();
        ViewComponent child;
        while(i.hasNext())
        {
            child = i.next();
            child.favoritesChanged(modelState);
        } 
    }
    
    public void gridMode()
    {
        Iterator<ViewComponent> i = children.iterator();
        ViewComponent child;
        while(i.hasNext())
        {
            child = i.next();
            child.gridMode();
        } 
    }

    public void slideShowMode()
    {
        Iterator<ViewComponent> i = children.iterator();
        ViewComponent child;
        while(i.hasNext())
        {
            child = i.next();
            child.slideShowMode();
        } 
    }
    // </editor-fold>
    
    // <editor-fold desc="overriden methods 'à la composite'...">
    @Override
    public void setController(Controller controller)
    {
        ///do what the superclass does...
        super.setController(controller);
        
        ///set the controller of the children...
        Iterator<ViewComponent> i = children.iterator();
        ViewComponent child;
        while(i.hasNext())
        {
            child = i.next();
            child.setController(controller);
        }
    }
    // </editor-fold>
    
}
